﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLVeChuyenBay
{
    public partial class frmTuyenBay : Form
    {
        public frmTuyenBay()
        {
            InitializeComponent();
        }

        TuyenBayBUS tbbus = new TuyenBayBUS();
        SanBayBUS sbbus = new SanBayBUS();
        TuyenBayDTO tbdto = new TuyenBayDTO();
        //TuyenBayBUS tbbus = new TuyenBayBUS();
        List<TuyenBayDTO> listtbdto = new List<TuyenBayDTO>();

        private void frmTuyenBay_Load(object sender, EventArgs e)
        {
            //List<TuyenBayDTO> listtbdto = new List<TuyenBayDTO>();
            listtbdto = tbbus.LayTuyenBay();
            for (int i = 0; i < listtbdto.Count; i++)
            {
                //dgvSanBay.Rows.Add(i + 1, ds[i].MaSanBay, ds[i].TenSanBay);
                dgvDSTuyenBay.Rows.Add(i + 1, listtbdto[i].MaTB, listtbdto[i].SanBayDi, listtbdto[i].SanBayDen);
            }
            List<SanBayDTO> lstSanBay1 = new List<SanBayDTO>();
            List<SanBayDTO> lstSanBay2 = new List<SanBayDTO>();
            lstSanBay1 = sbbus.GetAll();
            lstSanBay2 = sbbus.GetAll();
            cbSanBayDi.DataSource = lstSanBay1;
            cbSanBayDi.DisplayMember = "TenSanBay";
            cbSanBayDi.ValueMember = "MaSanBay";
            cbSanBayDi.SelectedIndex = -1;

            cbSanBayDen.DataSource = lstSanBay2;
            cbSanBayDen.DisplayMember = "TenSanBay";
            cbSanBayDen.ValueMember = "MaSanBay";
            cbSanBayDen.SelectedIndex = -1;
        }

        private void cbSanBayDi_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
           // TuyenBayDTO tbdto = new TuyenBayDTO();
           // TuyenBayBUS tbbus = new TuyenBayBUS();
            tbdto.MaTB = cbSanBayDi.SelectedValue.ToString() + "-" + cbSanBayDen.SelectedValue.ToString();
            tbdto.SanBayDi = cbSanBayDi.Text.ToString();
            tbdto.SanBayDen = cbSanBayDen.Text.ToString();
            int res = tbbus.ThemTuyenBay(tbdto);
            if (res == -1)
            {
                MessageBox.Show("Sân bay đi và sân bay đến không được trùng nhau!");
                return;
            }
            if (res >= 0)
            {
                dgvDSTuyenBay.Rows.Clear();
                //List<TuyenBayDTO> listtbdto = new List<TuyenBayDTO>();
                listtbdto = tbbus.LayTuyenBay();
                for (int i = 0; i < listtbdto.Count; i++)
                {
                    dgvDSTuyenBay.Rows.Add(i + 1, listtbdto[i].MaTB, listtbdto[i].SanBayDi, listtbdto[i].SanBayDen);
                }
                MessageBox.Show("Thêm tuyến bay thành công!");
            }
            else
                MessageBox.Show("Thêm tuyến bay thất bại!");
            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //TuyenBayDTO tbdto = new TuyenBayDTO();
            //TuyenBayBUS tbbus = new TuyenBayBUS();
            tbdto.MaTB = dgvDSTuyenBay.SelectedRows[0].Cells["MaTB"].Value.ToString();
            int res = tbbus.XoaTuyenBay(tbdto);
            if (res >= 0)
            {
                dgvDSTuyenBay.Rows.Clear();
                //List<TuyenBayDTO> listtbdto = new List<TuyenBayDTO>();
                listtbdto = tbbus.LayTuyenBay();
                for (int i = 0; i < listtbdto.Count; i++)
                {
                    dgvDSTuyenBay.Rows.Add(i + 1, listtbdto[i].MaTB, listtbdto[i].SanBayDi, listtbdto[i].SanBayDen);
                }
                MessageBox.Show("Xoá tuyến bay " + tbdto.MaTB + " thành công!");
            }
            else
                MessageBox.Show("Xoá tuyến bay thất bại!");
        }
    }
}
